package com.hooper.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Tim Hooper
 * @version 1.0
 * @time 2023/04/02/22:46
 */
@Aspect
@Component
@Slf4j
public class LoggingAspect {

    @Before("execution(* com.hooper.controller.*.*(..))")
    public void log(JoinPoint joinPoint){
        //获取request对象
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        // 获取方法名
        String name = joinPoint.getSignature().getName();
        // 获取参数
        Object[] args = joinPoint.getArgs();
        log.info("+=============================================+");
        log.info(String.format("--->方法名：%s", name));
        log.info("--->参数：{}", args);
        //请求方式
        log.info(String.format("--->请求方式：%s", request.getMethod()));
        log.info(String.format("--->请求路径：%s", request.getRequestURI()));
        //ip
        log.info(String.format("--->请求ip：%s", request.getRemoteAddr()));
        log.info("+=============================================+");
    }
}
